package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.TraderOrder;
import com.shxc.smarttrade.old.myinterface.PositionItemClickListener;
import com.shxc.smarttrade.old.utils.Arith;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/10.
 */

public class AllPositionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    LayoutInflater mInflater;
    Context mContext;
    List<TraderOrder> traderOrderList;
    private int tag = 0;// 标记是否是第一次进入
    private List<double[]> realTimeData;//0:market,1:ask,2:bid
    private PositionItemClickListener listener;
    private double[] realTimeProfit;

    public AllPositionAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<TraderOrder> traderOrderList, int tag, List<double[]> realTimeData, double[] realTimeProfit) {
        this.traderOrderList = traderOrderList;
        this.realTimeData = realTimeData;
        this.tag = tag;
        this.realTimeProfit = realTimeProfit;
        notifyDataSetChanged();
    }

    public void setOnPositionItemClickListener(PositionItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0)
            return new AllPositionViewholder(mInflater.inflate(R.layout.adapter_all_position, parent, false));
        else
            return new FootViewHolder(mInflater.inflate(R.layout.foot_view_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof AllPositionViewholder) {
            final TraderOrder traderOrder = traderOrderList.get(position);
            int digits = traderOrder.getDigits();
            double fee = Arith.add(traderOrder.getSwaps(), traderOrder.getCommission());

            ((AllPositionViewholder) holder).tvName.setText(traderOrder.getSymbolCn());
            ((AllPositionViewholder) holder).tvNameEn.setText(" " + traderOrder.getSymbolEn());
            if (traderOrder.getCmd() == 0) {
                ((AllPositionViewholder) holder).tvCmd.setImageResource(R.mipmap.icon_buy);
            } else {
                ((AllPositionViewholder) holder).tvCmd.setImageResource(R.mipmap.icon_sell);
            }
            ((AllPositionViewholder) holder).tvProfitPrice.setText(BaseUtils.getDigitsData(traderOrder.getOpenPrice(), traderOrder.getDigits()));
            ((AllPositionViewholder) holder).tvRealTimePrice.setText(BaseUtils.getDigitsData(traderOrder.getMarket(), digits) + "");
            ((AllPositionViewholder) holder).tvVolume.setText(traderOrder.getVolume() + "手");
//            ((AllPositionViewholder) holder).tvGearingxie.setText(traderOrder.getLever() + "");
            if (traderOrder.getIsCopy() == 0) {
                ((AllPositionViewholder) holder).tvFollowOrder.setVisibility(View.GONE);
            } else if (traderOrder.getIsCopy() == 1) {
                ((AllPositionViewholder) holder).tvFollowOrder.setVisibility(View.VISIBLE);
            }
            if (tag == 0) {
                if (traderOrder.getProfit() > 0)
                    ((AllPositionViewholder) holder).tvProfitLoss.setTextColor(mContext.getResources().getColor(R.color.tab_text));
                else
                    ((AllPositionViewholder) holder).tvProfitLoss.setTextColor(mContext.getResources().getColor(R.color.stock_fell_color));
                ((AllPositionViewholder) holder).tvProfitLoss.setText(BaseUtils.dealSymbol(Arith.add(traderOrder.getProfit(), fee)));

            } else {//实时计算盈亏
//                double priceTrans;
                double profit;//浮动盈亏
                double[] rdt = realTimeData.get(position);
                double realTP = Arith.add(realTimeProfit[position], fee);
                if (rdt != null) {
                    if (traderOrder.getCmd() == 0) {
                        ((AllPositionViewholder) holder).tvRealTimePrice.setText(BaseUtils.getDigitsData(rdt[2], digits) + "");

                    } else {
                        ((AllPositionViewholder) holder).tvRealTimePrice.setText(BaseUtils.getDigitsData(rdt[1], digits) + "");
                    }
                    if (realTP > 0) {
                        ((AllPositionViewholder) holder).tvProfitLoss.setTextColor(mContext.getResources().getColor(R.color.tab_text));
                    } else {
                        ((AllPositionViewholder) holder).tvProfitLoss.setTextColor(mContext.getResources().getColor(R.color.stock_fell_color));
                    }
                    ((AllPositionViewholder) holder).tvProfitLoss.setText(BaseUtils.dealSymbol(Arith.round(realTP, 2)));
                }
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (traderOrder != null)
                        listener.onPositionItemClick(traderOrder, position);
                }
            });
            ((AllPositionViewholder) holder).llClosePosition.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClosePositionClick(traderOrder, position);
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        return traderOrderList != null && traderOrderList.size() > 0 ? traderOrderList.size() + 1 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == traderOrderList.size() && traderOrderList.size() > 0)
            return 1;
        else
            return 0;
    }

    public class AllPositionViewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_cmd)
        ImageView tvCmd;
        @BindView(R.id.tv_profit_price)
        TextView tvProfitPrice;
        @BindView(R.id.tv_real_time_price)
        TextView tvRealTimePrice;
        @BindView(R.id.tv_volume)
        TextView tvVolume;
        //        @BindView(R.id.tv_gearingxie)
//        TextView tvGearingxie;
        @BindView(R.id.tv_profit_loss)
        TextView tvProfitLoss;
        @BindView(R.id.tv_name_en)
        TextView tvNameEn;
        @BindView(R.id.ll_close_position)
        LinearLayout llClosePosition;
        @BindView(R.id.tv_follow_order)
        TextView tvFollowOrder;

        AllPositionViewholder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder {
        FootViewHolder(View view) {
            super(view);
        }
    }
}
